package com.fetch.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置类，负责加载配置文件
 */
public class Configuration {

	private static Properties prop = new Properties();
	
	private Configuration(){}
	
	/**
	 * 加载配置文件
	 * @return
	 */
	public static boolean load(){
		try{
			InputStream is = null;
			try{
				is = Configuration.class.getClassLoader().getResourceAsStream("conf.properties");
			}catch (Exception e) {}
			if(null == is){
				is = new FileInputStream(new File("conf.properties"));
			}
			prop.load(is);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("配置文件conf.properties加载失败");
			return false;
		}
	}
	
	public static String getString(String key){
		return prop.getProperty(key);
	}
	
	public static String getString(String key, String defaultValue){
		return prop.getProperty(key, defaultValue);
	}
	
	public static Long getLong(String key){
		String val = prop.getProperty(key);
		if(StringUtils.isNotEmpty(val)){
			return Long.parseLong(val);
		}
		return null;
	}
	
	public static Long getLong(String key, long defaultValue){
		Long val = getLong(key);
		return (null!=val ? val :defaultValue);
	}
	
	public static Integer geInt(String key){
		String val = prop.getProperty(key);
		if(StringUtils.isNotEmpty(val)){
			return Integer.parseInt(val);
		}
		return null;
	}
	
	public static Integer getLong(String key, int defaultValue){
		Integer val = geInt(key);
		return (null!=val ? val :defaultValue);
	}
	
}
